package yanjunwei.rbac.service.array;

import yanjunwei.rbac.domain.dto.DeptDto;
import yanjunwei.rbac.domain.entity.Dept;
import yanjunwei.rbac.domain.vo.DeptVo;
import yanjunwei.rbac.domain.vo.Result;
import yanjunwei.rbac.service.IDeptService;
import yanjunwei.rbac.service.IUserService;
import yanjunwei.rbac.util.SingletonFactory;

import java.time.LocalDateTime;

@Deprecated
public class DeptService extends BaseService <Dept,DeptDto,DeptVo>implements IDeptService {

    private IUserService userService;


    private DeptService(){
    data=new Dept[DATA_SIZE+1];
    temp=new DeptVo[DATA_SIZE];
    }


@Override
    DeptVo convertToVo(Dept dept) {
        return new DeptVo(dept.getId(), dept.createAt,dept.updateAt,dept.name, dept.phone);
    }
@Override
    Dept createEntity(DeptDto dto,int id){
    LocalDateTime now=LocalDateTime.now();
        return new Dept(id,now,now,dto.name,dto.phone);
    }
    @Override
    void updateEntity(DeptDto dto,Dept dept){
        dept.updateAt=LocalDateTime.now();
        dept.name=dto.name;
        dept.phone=dto.phone;
    }



@Override
    public Result delete(int id){
        if (!existsById(id)){
        return Result.failure("删除失败，不存在该数据");}

        if (userService==null){
            userService= SingletonFactory.getInstance(UserService.class);
        }

        if (userService.deptInUse(id)){
            return Result.failure("删除失败，还有用户属于该部门，不能删除");
        }
        data[id]=null;
        return Result.success("删除成功",null);
    }
}
